package guesswords.khova.com;


import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;


import android.view.KeyEvent;

public class GuessWordzGame extends BaseGameActivity
{
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;
	
	/* Set the Position of the Game Guess Board */
	private static final int GAMEBOARD_POSX = 10;
	private static final int GAMEBOARD_POSY = 10;
	
	private static final int SCROLLBLOCK_POSX = 10;
	private static final int SCROLLBLOCK_POSY = 60;
	

	
	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;

	private GameBoard theGame = new GameBoard(); 
	private GfxScrollBlock gfxScrollBlock = new GfxScrollBlock();
	private GfxGameBoard gfxGameBoard = new GfxGameBoard();
	private TXTGameBoard txtGameBoard = new TXTGameBoard();
	private TXTScrollBlock txtScrollBlock = new TXTScrollBlock();
	private InputGameBoard inputGuessWordz = new InputGameBoard();
	
	
	
	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	
	@Override
	public Engine onLoadEngine() {
		
		// Set up Camera
		
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		
		MLog.i(GuessWordz.TAG, "Engine Loaded");
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera).setNeedsSound(true));
	}
	
	@Override
	public void onLoadResources() {
			
		/* Load Game Engine */
		theGame.loadPuzzle();
		MLog.i(GuessWordz.TAG, "Puzzle Loaded");
		
		/* Load and Store Graphical Resources for Game Board */
		MLog.i(GuessWordz.TAG, "Loading Textures");
		this.txtGameBoard.mTexture = new Texture(512,512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.txtGameBoard.mLetterBlockTextureRegion = TextureRegionFactory.createTiledFromAsset(this.txtGameBoard.mTexture, this, "gfx/game_letter_blocks.png", 0, 0, 5,7);
		MLog.i(GuessWordz.TAG, "Loading Textures: GameBoardGFX Loaded");
		this.txtGameBoard = gfxGameBoard.loadTexture(this.txtGameBoard);
		
		/* Load and Store Graphical Resources for Scroll Block */
		MLog.i(GuessWordz.TAG, "Loading Scroll Block Textures");
		this.txtScrollBlock.mScrollBlockTexture = new Texture(512,512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.txtScrollBlock.mScrollBlockTextureRegion = TextureRegionFactory.createTiledFromAsset(this.txtScrollBlock.mScrollBlockTexture, this, "gfx/gfx_letters.png",0,0,5,6);
		this.gfxScrollBlock.setTexture(this.txtScrollBlock.mScrollBlockTexture, this.txtScrollBlock.mScrollBlock);
		MLog.i(GuessWordz.TAG, "Loading Textures: ScrollBlockGFX Loaded");
		

		
		this.mEngine.getTextureManager().loadTextures(this.txtGameBoard.mTexture, this.txtScrollBlock.mScrollBlockTexture);
		MLog.i(GuessWordz.TAG, "Resources Loaded");
	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

	    Scene scene = new Scene();
		scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

		gfxGameBoard.setUIPosition(GAMEBOARD_POSX, GAMEBOARD_POSY); // Set Position of GameBoard
		gfxScrollBlock.setUIPosition(SCROLLBLOCK_POSX, SCROLLBLOCK_POSY);
		
		theGame = gfxGameBoard.buildGameBlocks(theGame);
		theGame = gfxGameBoard.assignGameBlockSprites(theGame);
		
		
		/* Draw Board to Screen */
		MLog.i(GuessWordz.TAG, "Loading Scene");
		scene = gfxGameBoard.loadDisplay(scene, theGame);
		scene = gfxScrollBlock.loadDisplay(scene, theGame);

		
		MLog.i(GuessWordz.TAG, "Scene Loaded");
		
		// Setting update Update Handler
	    scene.registerUpdateHandler(new IUpdateHandler() {
	    	@Override
			public void onUpdate(final float pSecondsElapsed) {
	    		
	    		if (theGame.doRefresh)
	    		{
	    			theGame = gfxGameBoard.assignGameBlockSprites(theGame);
	    			refreshScene();
	    			
	    			theGame.doRefresh = false;
	  
	    		}
	    	}
	    	@Override
			public void reset() {}
	    });
	    
		return scene;
				
	}
	


	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
	       
		theGame = inputGuessWordz.checkGameBoardInput(pKeyCode,99, theGame);
		
	                return super.onKeyDown(pKeyCode, pEvent); //similarily, this will allow actions other than key press to be processed elsewhere.
	}
	@Override
	public void onLoadComplete() {

	}
	


	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
	private void refreshScene()
	{
		Scene scene = this.mEngine.getScene();
		//scene
		scene = gfxGameBoard.loadDisplay(scene, theGame);
		scene = gfxScrollBlock.loadDisplay(scene, theGame);
		MLog.i(GuessWordz.TAG, "Scene Refresh Updated");
		
		
	}
	
}